<?php
	@session_start ();
	include("includes/secure.php");
	if ( $_SESSION['use_type'] != 'Admin')
		echo "<script> window.location.href='logout.php'; </script>";
	
	include("includes/config.php");
	include("includes/class/user.class.php");
	require("includes/class/RijndaelHex.php");
	
	$user = new User(); 
	$rijndaelHex = new RijndaelHex();
	
	$use_code = (int) $_GET['use_code'];
	
	$msg_info = $msg_alert = $msg_error = false;
	$_SESSION['msg_cool'] = '';
	$show_msg = 0;
	
	$data = $user->getUserData($use_code);
	$telephones = $user->getTelephones($use_code);
	$countTelephones = count ($telephones);
	
	if ( $countTelephones > 1){
		$style_tr_phones = '';
		$disabled_less_button = '';
	}
	else{
		$style_tr_phones = 'style="display:none"';
		$disabled_less_button = 'disabled';
	}
	
	if(isset($_POST['edit']) && $_POST['edit'] == 1){
		$new_data->edit_code		= $_POST['use_code'];
		$new_data->use_name 		= trim(mysql_real_escape_string($_POST['use_name']));
		$new_data->use_lastname 	= trim(mysql_real_escape_string($_POST['use_lastname']));
		$new_data->use_email    	= trim(mysql_real_escape_string($_POST['use_email']));
		$new_data->use_login 		= trim(mysql_real_escape_string($_POST['use_login']));
		$new_data->use_password 	= md5($new_data->use_login.mysql_real_escape_string($_POST['use_password']).$new_data->use_login);		
		$new_data->use_type 		= trim(mysql_real_escape_string($_POST['use_type']));
			
		
		if($_POST['use_login'] != $_POST['login_ant'] || $_POST['use_password'] != '')
			$new_data->use_password = $rijndaelHex->linencrypthex($new_data->use_login.mysql_real_escape_string($_POST['use_password']).$new_data->use_login);
		else
			$new_data->use_password = mysql_real_escape_string($_POST['passAnt']);		

		if($user->editUser($new_data)){
			$_SESSION["msg_cool"] .= 'El usuario fue modificado con exito!!..<br />';
			
			$data->id_fk = $new_data->edit_code;
			
			if ( $user->deleteTelephones($data->id_fk) ){
				$countTel_number = count ( $_POST['tel_number'] );
				$i = 0;
				while ( $i < $countTel_number ){
					$data->tel_number = $_POST['tel_number'][$i];
					$data->tel_ext = $_POST['tel_ext'][$i];
					
					if ( ! $user->addTelephone($data) ){
						$msg_alert = true;
						$_SESSION["msg_cool"] .= 'Error adicionando telefonos.<br />';
					}
						
					$i ++;
				}
				
				echo "<script> window.location.href='listUser.php?show_msg=3'</script>";
			}
			else{
				$_SESSION["msg_cool"] .= 'Error modificando telefonos.<br />';
				echo "<script> window.location.href='listUser.php?show_msg=1'</script>";	
			}
		}
		else{
			$msg_error = true;
			$_SESSION["msg_cool"] .= 'No se pudo modificar el usuario.<br />';
		}
		
		echo "<script> window.location.href='listUser.php'</script>";
	}
?>